package com.atguigu.eduorder.service.impl;

import com.atguigu.commonutils.order.CourseWebVoOrder;
import com.atguigu.commonutils.order.UcenterMemberOrder;
import com.atguigu.eduorder.client.EduClient;
import com.atguigu.eduorder.client.UcenterClient;
import com.atguigu.eduorder.entity.Order;
import com.atguigu.eduorder.mapper.OrderMapper;
import com.atguigu.eduorder.service.OrderService;
import com.atguigu.eduorder.utils.OrderNoUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-22
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private EduClient eduClient;

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public String createOrders(String courseId, String memberIdByJwtToken) {

        //根据远程调用获取用户信息
        UcenterMemberOrder userinfoOrder = ucenterClient.getUserinfoOrder(memberIdByJwtToken);

        //根据远程调用获取课程信息
        CourseWebVoOrder courseInfoOrder = eduClient.getCourseInfoOrder(courseId);
        //创建订单
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());//订单id
        order.setCourseId(courseId);//课程Id
        order.setCourseTitle(courseInfoOrder.getTitle());//课程标题
        order.setCourseCover(courseInfoOrder.getCover());//课程封面
        order.setTeacherName(courseInfoOrder.getTeacherName());//讲师姓名
        order.setTotalFee(courseInfoOrder.getPrice());//课程价格
        order.setMemberId(memberIdByJwtToken);//用户id
        order.setMobile(userinfoOrder.getMobile());//用户手机号
        order.setNickname(userinfoOrder.getNickname());//用户昵称
        order.setStatus(0);//支付状态
        order.setPayType(1);//支付类型(微信)
        baseMapper.insert(order);
        //返回订单号
        return order.getOrderNo();
    }
}
